﻿Public Class HeSo
    Private sta As Boolean = False
    Dim data As DataTable
    Dim xlhs As XLHeSo
    Dim dsHs As ArrayList
    Dim vt As Integer
    Dim ds As New List(Of DTO_NhanVienCT.NhanVien)
    Private Sub HeSo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Rload()
    End Sub
    Private Sub Rload()
        data = New DataTable
        xlhs = New XLHeSo
        dsHs = New ArrayList
        dsHs = xlhs.BUS_ArrHS()
        data = xlhs.BUS_DSHS
        ds = xlhs.BUS_DSNV()
        showHS()
    End Sub
    Private Sub showHS()
        If data.Rows.Count < 0 Then Return
        lv_hesonv.Items.Clear()
        lv_hesonv.View = View.Details
        lv_hesonv.GridLines = True
        lv_hesonv.FullRowSelect = True
        For Each dr As DataRow In data.Rows
            Dim item As New ListViewItem
            item.Text = "STT"
            item.SubItems.Add(dr("MaNV"))
            item.SubItems.Add(xlhs.BUS_TimNV(dr("MaNV"), ds))
            item.SubItems.Add(Convert.ToString(dr("HeSo")))
            lv_hesonv.Items.Add(item)
            CapNhatSTT()
        Next
        Cb_HeSo.Items.Clear()
        For Each str As String In dsHs
            Cb_HeSo.Items.Add(str)
        Next
        Cb_HeSo.SelectedIndex = 0
    End Sub
    Private Sub CapNhatSTT()
        For i As Integer = 1 To lv_hesonv.Items.Count
            lv_hesonv.Items(i - 1).Text = i.ToString
        Next
    End Sub

    Private Sub bt_lammoi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_lammoi.Click
        xlhs = New XLHeSo
        xlhs.BUS_LamMoi(data, ds)
        showHS()
    End Sub

    Private Sub HeSo_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        Dim dr As DialogResult = MessageBox.Show("Bạn có muốn lưu dữ liệu lại trước khi đóng màn hình này không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If dr = Windows.Forms.DialogResult.Yes Then
            Dim xl As New XLHeSo
            If xl.BUS_SaveHS(data) = True Then
                MessageBox.Show("Lưu Thành Công ", "Thông Báo", MessageBoxButtons.OK)
            End If
        End If
    End Sub

    Private Sub lv_hesonv_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lv_hesonv.SelectedIndexChanged
        Try
            xlhs = New XLHeSo
            vt = lv_hesonv.SelectedItems(0).Index
            tb_MaNV.Text = lv_hesonv.SelectedItems(0).SubItems(1).Text
            tb_HoTen.Text = lv_hesonv.SelectedItems(0).SubItems(2).Text
            If lv_hesonv.SelectedItems(0).SubItems(3).Text <> "" Then
                Cb_HeSo.Text = lv_hesonv.SelectedItems(0).SubItems(3).Text
            End If
            sta = True
        Catch ex As Exception
        End Try
    End Sub

    Private Sub bt_capnhat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_capnhat.Click
        If sta = True Then
            For Each dr As DataRow In data.Rows
                If dr("MaNV") = lv_hesonv.SelectedItems(0).SubItems(1).Text Then
                    dr("HeSo") = Cb_HeSo.Text
                    lv_hesonv.Items(vt).SubItems(3).Text = Cb_HeSo.Text
                    Exit For
                End If
            Next
            RFesh()
        Else
            MessageBox.Show("Không có đối tượng cần cập nhật", "Thông Báo", MessageBoxButtons.OK)
        End If
    End Sub

    Private Sub bt_lamlai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_lamlai.Click
        RFesh()
    End Sub
    Private Sub RFesh()
        Cb_HeSo.SelectedIndex = 0
        sta = False
        tb_HoTen.Text = String.Empty
        tb_MaNV.Text = String.Empty
    End Sub

End Class